﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;

namespace eProject_SEM3_SecurityServices.Presentations
{
    public partial class frmService : System.Web.UI.Page
    {
        BService _bService = new BService();
        OService _oService = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._bService = new BService();
                this.BindData();
                //this.pnlNew.Visible = false;
            }
        }
        protected void BindData()
        {
            if (this._bService != null)
            {
                if (this._bService.getServiceList() != null)
                {
                    this.grvServices.DataSource = this._bService.getServiceList();
                    this.grvServices.DataBind();
                }

            }
        }
        protected void AddNewService()
        {
            _oService = new OService();
            _oService.ServiceName = txtName.Text.Trim();
            _oService.ServiceDescription = txtServiceDes.Text.Trim();
            if (_bService != null)
            {
                if (_bService.AddNewService(_oService))
                {
                    this.BindData();
                    Response.Write("<script>alert('A new service is added);</script>");
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.AddNewService();
        }

        protected void grvServices_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvServices.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void grvServices_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvServices.EditIndex = e.NewEditIndex;
            this.BindData();
        }

        protected void grvServices_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvServices.EditIndex = -1;
            this.BindData();
        }

        protected void grvServices_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int id = int.Parse(grvServices.DataKeys[e.RowIndex].Value.ToString());
                if (_bService.DeleteService(id))
                {
                    Response.Write("<script>alert('Delete successfull!')</script>");                   
                }
                this.BindData();
            }
            catch (Exception)
            {
                this.BindData();              
            }
        }

        protected void grvServices_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int id = int.Parse(grvServices.DataKeys[e.RowIndex].Value.ToString());
                GridViewRow row = (GridViewRow)grvServices.Rows[e.RowIndex];
                TextBox txtName = row.FindControl("txtServiceName") as TextBox;
                TextBox txtDes = row.FindControl("txtDes") as TextBox;
                _oService = new OService();
                _oService.ServiceId = id;
                _oService.ServiceName = txtName.Text.Trim();
                _oService.ServiceDescription = txtDes.Text.Trim();
                if (_bService != null)
                {
                    if (_bService.UpdateService(_oService))
                    {
                        Response.Write("<script>alert('Update successfull!')</script>");                      
                    }                   
                }
                grvServices.EditIndex = -1;
                this.BindData();
            }
            catch (Exception ex)
            {
                grvServices.EditIndex = -1;
                this.BindData();
            }
        }
    }
}